<template>
  <common-edit-dialog
    :title="dialogTitle"
    :visible.sync="dialogShow"
    @cancel="close"
    @submit="submitForm"
    :button-loading="isPersistenceLoading"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="18">
        <el-col :span="12">
          <el-form-item label="字段名称" prop="columnName">
            <common-input
              v-model="form.columnName"
              placeholder="请输入字段名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段别名" prop="columnAlias">
            <common-input
              v-model="form.columnAlias"
              placeholder="请输入字段别名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段类型" prop="columnType">
            <el-select
              v-model="form.columnType"
              placeholder="请选择字段类型"
              filterable
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in typeList"
                :key="index"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="时间格式" prop="dateFormat">
            <el-select
              v-model="form.dateFormat"
              placeholder="请选择时间格式"
              filterable
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in dateTypeList"
                :key="index"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item
          >
        </el-col>
      </el-row>
    </el-form>
  </common-edit-dialog>
</template>

<script>
  import {
    repertory_column_add,
    repertory_column_edit,
    repertory_column_datatypeList
  } from '@/api/management/echart/repertory'

  export default {
    components: {},
    data() {
      return {
        isPersistenceLoading: false,
        dialogTitle: '新增', // 标题
        dialogShow: false, // 是否显示
        typeList: [],
        dateTypeList: [
          {
            label: 'yyyy-mm-dd hh:mm:ss',
            value: 'yyyy-mm-dd hh:mm:ss'
          },
          {
            label: 'yyyy-mm-dd',
            value: 'yyyy-mm-dd'
          }
        ],

        form: {
          indexId: null,
          columnName: '',
          columnAlias: '',
          columnType: '',
          dateFormat: ''
        },
        rules: {
          columnName: [
            { required: true, message: '请输入字段名称', trigger: 'blur' },
            { max: 80, message: '输入字符过长', trigger: 'blur' }
          ],
          columnAlias: [
            { required: true, message: '请输入字段别名', trigger: 'blur' },
            { max: 80, message: '输入字符过长', trigger: 'blur' }
          ],
          columnType: [
            { required: true, message: '请选择字段类型', trigger: 'blur' }
          ],
          dateFormat: [
            { required: true, message: '请选择时间格式', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {},
    computed: {},
    methods: {
      /**
       * 关闭
       */
      close() {
        this.$refs.form.resetFields()
        this.dialogShow = false
        this.$emit('refresh')
      },
      /**
       * 显示
       */
      show(data) {
        this.dialogTitle = data.id ? '编辑字段' : '新增字段'
        this.dialogShow = true
        this.form = data.id ? data : this.$options.data().form
        this.form.indexId = data.indexId
      },
      /**
       * 提交
       */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const data = { ...this.form }
            if (data.id) {
              if (!this.isPersistenceLoading) {
                this.isPersistenceLoading = true
                repertory_column_edit(data)
                  .then((res) => {
                    if (res && res.code === 200) {
                      this.$GlobalSuccessMessage('编辑成功')
                      this.close()
                    } else {
                      this.$GlobalErrorConfirm('编辑失败', res.msg)
                    }
                  })
                  .finally(() => {
                    this.isPersistenceLoading = false
                  })
              }
            } else {
              if (!this.isPersistenceLoading) {
                this.isPersistenceLoading = true
                repertory_column_add(data)
                  .then((res) => {
                    if (res && res.code === 200) {
                      this.$GlobalSuccessMessage('新增成功')
                      this.close()
                    } else {
                      this.$GlobalErrorConfirm('新增失败', res.msg)
                    }
                  })
                  .finally(() => {
                    this.isPersistenceLoading = false
                  })
              }
            }
          }
        })
      }
    },
    created() {
      repertory_column_datatypeList({}).then((res) => {
        this.typeList = res.data
      })
    },
    mounted() {
    }
  }
</script>
<style lang="scss" scoped>
</style>
